<?php

namespace zhtfrontend\models\datas;

use Yii;
use common\components\extenders\MenuManage;

/**
 * This is the model class for table "{{%settlement}}".
 *
 * @property integer $settlement_id
 * @property string $title
 * @property integer $mechanism_id
 * @property string $price
 * @property integer $state
 * @property integer $user_id
 * @property integer $created_at
 * @property integer $updated_at
 * @property string $voucher
 */
class Settlement extends \zhtfrontend\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%settlement}}';
    }


    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['mechanism_id', 'state', 'user_id', 'created_at', 'updated_at'], 'integer'],
            [['price'], 'number'],
            [['title', 'voucher'], 'string', 'max' => 255]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'settlement_id' => Yii::t('zhtfrontend', '结算编号'),
            'title' => Yii::t('zhtfrontend', '结算标题'),
            'mechanism_id' => Yii::t('zhtfrontend', '机构编号'),
            'price' => Yii::t('zhtfrontend', '结算金额'),
            'state' => Yii::t('zhtfrontend', '结算状态'),
            'user_id' => Yii::t('zhtfrontend', '结算人'),
            'created_at' => Yii::t('zhtfrontend', '结算时间'),
            'updated_at' => Yii::t('zhtfrontend', '修改时间'),
            'voucher' => Yii::t('zhtfrontend', '付款凭证'),
        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        return $fields;
    }

    public function extraFields()
    {
        return [
            'order_sn' => function () {
                return $this->settlementRelation[0]->order->order_id;
            },
            'order_update_at' => function () {
                return date('Y-m-d H:i', $this->settlementRelation[0]->order->create_at);
            },
            'sale_title' => function () {
                return isset($this->settlementRelation[0]->orderSales->title) ? $this->settlementRelation[0]->orderSales->title : '';
            },
            'mechanic_name' => function () {
                return !empty($this->mechanic->name) ? $this->mechanic->name : '';
            },
            'distribution_proportion' => function () {
                return $this->settlementRelation[0]->orderSales->distribution_proportion;
            },
            'static_name' => function () {
                return ($this->state == 0) ? '未付款' : '已付款';
            },
            'month' => function () {
                return Yii::$app->formatter->asDate($this->created_at, 'php:m') . '月';
            },
            'name' => function () {
                return $this->admin->name;
            },
            'operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->isDisable(
                    ['delete' => function ($data) {
                        if ($data['state'] == 1) {
                            return '<span style="color:#ccc">删除</span>';
                        } else {
                            return '';
                        }
                    },
                        'state' => function ($data) {
                            if ($data['state'] == 1) {
                                return '<span style="color:#ccc">结算</span>';
                            } else {
                                return '';
                            }
                        },
                        'view' => function ($data) {
                            return (new MenuManage(MenuManage::TOP_MENU))->detailsMenu('dividedinto', 'view', false, ['rel' => 'dialog-chose', 'data' => ['id' => $data['settlement_id']]]);
                        }
                    ]
                )->create();
            },
        ];
    }

    public function getSettlementRelation()
    {
        return $this->hasMany(SettlementRelation::className(), ['settlement_id' => 'settlement_id']);
    }

    public function getMechanic()
    {
        return $this->hasOne(Mechanic::className(), ['id' => 'mechanism_id']);
    }

    public function getAdmin()
    {
        return $this->hasOne(Admin::className(), ['id' => 'user_id']);
    }

}
